﻿//-----------------------------------------------------------------------
// <copyright file="CookieHelper.cs" company="O-RID">
//     Copyright (c)2010 O-RID Co.,Ltd.
// </copyright>
// <author>zhangjun</author>
// <summary>
//         COOKIES操作方法集
//          
//          作成日: 07/21/2010
//</summary>
//-----------------------------------------------------------------------
namespace Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList
{
    using System;
    using System.Windows.Browser;
    using System.Collections.Generic;

    /// <summary>
    /// COOKIES操作方法集
    /// </summary>
    public class CookieHelper
    {
        #region 根据KEY值读 COOKIE的Value值
        /// <summary>
        /// 根据KEY值读 COOKIE的Value值   
        /// </summary>
        /// <param name="key">要读取的KEY值</param>
        /// <returns>对应的Value值</returns>
        public static string GetCookies(string key)
        {
            key += '=';
            //取出所有的COOKIES
            string[] cookies = HtmlPage.Document.Cookies.Split(':');
            //遍历
            foreach (string cookie in cookies) 
            {
                string cookieStr = cookie.Trim();
                //取COOKIE的KEY名称的位置
                if (cookieStr.StartsWith(key, StringComparison.OrdinalIgnoreCase))
                {
                    //分隔出KEY的值
                    string[] vals = cookieStr.Split('=');

                    if (vals.Length >= 2) 
                    {
                        return vals[1];
                    }
                    return string.Empty;
                }
            }
            return string.Empty;
        }
        #endregion 根据KEY值读 COOKIE的Value值

        #region 读取COOKIE的KEY和VALUE值
        /// <summary>
        /// 读取COOKIE的KEY和VALUE值
        /// </summary>
        /// <returns> list[0]:key list[1]:value </returns>
        public static List<string> GetCookies()
        {
            List<string> retlist = new List<string>();

            //读取 本地 COOKIE

            if (!string.IsNullOrEmpty(HtmlPage.Document.Cookies))
            {
                String[] cookiestr = HtmlPage.Document.Cookies.Split(';');
                foreach (String cookie in cookiestr)
                {
                    String[] keyValues = cookie.Split('=');
                    if (keyValues.Length == 2)
                    {
                        if (!string.IsNullOrEmpty(keyValues[0].Trim()))
                        {
                            retlist.Add(keyValues[0].ToString().Trim());

                        }
                        else
                        {
                            retlist.Add(string.Empty);
                        }

                        if (!string.IsNullOrEmpty(keyValues[1].Trim()))
                        {
                            retlist.Add(keyValues[1].ToString().Trim());
                        }
                        else
                        {
                            retlist.Add(string.Empty);
                        }
                    }
                }
            }
            else 
            {
                retlist.Add(string.Empty);
                retlist.Add(string.Empty);
            }
            return retlist;
        }
        #endregion 读取COOKIE的KEY和VALUE值

        #region  把用户信息写入COOKIES
        /// <summary>
        /// 把用户信息写入COOKIES 
        /// </summary>
        /// <param name="key">key值</param>
        /// <param name="value">Value值</param>
        public static void SetCookie(string key, string value)
        {
            // 把用户信息写入COOKIES
            DateTime date = DateTime.UtcNow + TimeSpan.FromDays(7);
            String cookie = String.Format("{0}={1};expires={2}",
                                            key,
                                            value,
                                            date.ToString("R"));
            HtmlPage.Document.SetProperty("cookie", cookie);
        }
        #endregion   把用户信息写入COOKIES

        #region  删除 COOKIES信息
        /// <summary>
        ///  删除 指定的 cookie 信息
        /// </summary>
        /// <param name="key"></param>
        public static void DeleteCookie(String key)
        {
            DateTime expir = DateTime.UtcNow - TimeSpan.FromDays(1);
            string cookie = String.Format("{0}=;expires={1}",
                key, expir.ToString("R"));
            HtmlPage.Document.SetProperty("cookie", cookie);
        }
        #endregion 删除 COOKIES信息

    }
   
}
